
///////////////////////////////////////////////////////////////////////////////   
#include "stm8l15x.h"
#include "ring.h"   
  
   
///----------------------------------------------------------------------------   
 // Ring buffer init functin.   
///----------------------------------------------------------------------------   
int RbInit(pR_buf rb, unsigned char *pBuf, unsigned int bLen)   
{   
    if (rb == NULL || pBuf == NULL || bLen < 1)   
    {   
        return FALSE;   
    }   
   
    OS_DISABLE_INT();                   //Close all interrupt   
    rb->head = 0;   
    rb->end = 0;   
    rb->buf = pBuf;   
    rb->RbLen = bLen;   
    OS_ENABLE_INT();                    //Open all interrupt   
   
    return TRUE;   
}   
   
///----------------------------------------------------------------------------   
 // Ring buffer reinit functin.   
///----------------------------------------------------------------------------   
int RbReinit(pR_buf rb)   
{   
    if (rb == NULL)   
    {   
        return FALSE;   
    }   
   
    OS_DISABLE_INT();   
    rb->head = 0;   
    rb->end = 0;   
    OS_ENABLE_INT();   
   
    return TRUE;   
}   
   
///----------------------------------------------------------------------------   
 // Insert one data to ring buf.   
///----------------------------------------------------------------------------   
int RbPush(pR_buf rb, unsigned char val)   
{   
    if (RbFull(rb))   
    {   
        return FALSE; 
		
    }   
       
    OS_DISABLE_INT();   
    rb->buf[rb->head] = val;   
    rb->head = (rb->head + 1) % rb->RbLen;   
    OS_ENABLE_INT();   
   
    return TRUE;   
}   
   
///----------------------------------------------------------------------------   
 // Remove one data from ring buf.   
///----------------------------------------------------------------------------   
int RbPop(pR_buf rb, unsigned char *val)   
{   
    if (RbEmpty(rb))   
    {   
        return FALSE; 
	
    }   
       
    OS_DISABLE_INT();   
    *val = rb->buf[rb->end];   
    rb->end = (rb->end + 1) % rb->RbLen;   
    OS_ENABLE_INT();   
   
    return TRUE;   
}   

   
///----------------------------------------------------------------------------   
 // Insert some data to ring buf.   
///----------------------------------------------------------------------------   
int RbInsrt(pR_buf rb, unsigned char *in, int inLen)   
{   
    if (rb == NULL || in == NULL || inLen > RbGetFree(rb))   
    {   
        return FALSE;   
		
    }   
   
    OS_DISABLE_INT();   
    while(inLen--)   
    {   
        rb->buf[rb->head] = *in++;   
        rb->head = (rb->head + 1) % rb->RbLen;   
    }   
    OS_ENABLE_INT();   
   
    return TRUE;   
}   
   
///----------------------------------------------------------------------------   
 // Remove some data from ring buf.   
///----------------------------------------------------------------------------   
int RbRemove(pR_buf rb, unsigned char *out, int outLen)   
{   
    if (rb == NULL || out == NULL || outLen > RbGetDatLen(rb))   
    {   
        return FALSE;   
    }   
       
    OS_DISABLE_INT();   
    while(outLen--)   
    {  
	
        *out++ = rb->buf[rb->end];   
        rb->end = (rb->end + 1) % rb->RbLen; 
	
	
    }   
    OS_ENABLE_INT();   
   
    return TRUE;   
}   
   
///----------------------------------------------------------------------------   
 // Search one char from ring buf.   
///----------------------------------------------------------------------------   
int RbSearch(pR_buf rb, unsigned char ch, char newSearch)   
{   
    static unsigned int t_end = 0;   
       
    if (rb == NULL)   
    {   
        return -1;   
    }   
       
    if (newSearch)   
    {   
        t_end = rb->end;   
    }   
       
    OS_DISABLE_INT();   
    while((t_end != rb->head) && (rb->buf[t_end] != ch))   
    {   
        t_end = (t_end + 1) % rb->RbLen;   
    }   
    OS_ENABLE_INT();   
       
    if (t_end == rb->head)   
    {   
        return -1;   
    }   
   
    return t_end;   
}   
   